﻿using NhWeather.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace NhWeather;

public class SettingsViewModel : INotifyPropertyChanged
{
    private bool _isAutoAddDevcie;
    private bool _isAutoAdd03Reg;
    private bool _isAutoAdd04Reg;
    private bool _isAutoAdd06Reg;
    private bool _isAutoAdd16Reg;
    private bool _isDefaultRecorder;
    private string _com;
    private string _handshakeFrame;

    public SettingsViewModel()
    {
        // 从配置文件中读取初始值
        _isAutoAddDevcie = Settings.Default.IsAutoAddDevcie;
        _isAutoAdd03Reg = Settings.Default.IsAutoAdd03Reg;
        _isAutoAdd04Reg = Settings.Default.IsAutoAdd04Reg;
        _isAutoAdd06Reg = Settings.Default.IsAutoAdd06Reg;
        _isAutoAdd16Reg = Settings.Default.IsAutoAdd16Reg;
        _isDefaultRecorder = Settings.Default.IsDefaultRecorder;
        _com = Settings.Default.COM;
        _handshakeFrame = Settings.Default.HandshakeFrame;
    }

    public bool IsAutoAddDevcie
    {
        get => _isAutoAddDevcie;
        set
        {
            if (_isAutoAddDevcie != value)
            {
                _isAutoAddDevcie = value;
                Settings.Default.IsAutoAddDevcie = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    public bool IsAutoAdd03Reg
    {
        get => _isAutoAdd03Reg;
        set
        {
            if (_isAutoAdd03Reg != value)
            {
                _isAutoAdd03Reg = value;
                Settings.Default.IsAutoAdd03Reg = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    public bool IsAutoAdd04Reg
    {
        get => _isAutoAdd04Reg;
        set
        {
            if (_isAutoAdd04Reg != value)
            {
                _isAutoAdd04Reg = value;
                Settings.Default.IsAutoAdd04Reg = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    public bool IsAutoAdd06Reg
    {
        get => _isAutoAdd06Reg;
        set
        {
            if (_isAutoAdd06Reg != value)
            {
                _isAutoAdd06Reg = value;
                Settings.Default.IsAutoAdd06Reg = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    public bool IsAutoAdd16Reg
    {
        get => _isAutoAdd16Reg;
        set
        {
            if (_isAutoAdd16Reg != value)
            {
                _isAutoAdd16Reg = value;
                Settings.Default.IsAutoAdd16Reg = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    public bool IsDefaultRecorder
    {
        get => _isDefaultRecorder;
        set
        {
            if (_isDefaultRecorder != value)
            {
                _isDefaultRecorder = value;
                Settings.Default.IsDefaultRecorder = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    public string COM
    {
        get => _com;
        set
        {
            UpdateProperty(ref _com, value, () =>
            {
                Settings.Default.COM = value;
                Settings.Default.Save();
            });
        }
    }

    public string HandshakeFrame
    {
        get => _handshakeFrame;
        set
        {
            if (_handshakeFrame != value)
            {
                _handshakeFrame = value;
                Settings.Default.HandshakeFrame = value;
                Settings.Default.Save();
                OnPropertyChanged();
            }
        }
    }

    #region 属性通知
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    /// <summary>
    /// 更新属性并触发属性更改通知
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="properValue"></param>
    /// <param name="newValue"></param>
    /// <param name="onChanged">如要更改属性要运行的方法</param>
    /// <param name="propertyName"></param>
    protected void UpdateProperty<T>(ref T properValue, T newValue, Action onChanged = null, [CallerMemberName] string propertyName = null)
    {
        if (Equals(properValue, newValue))
        {
            return;
        }
        properValue = newValue;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
    }
    #endregion
}